package com.zfzs.post.modules.post.service.impl;

import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.model.vo.TerminalInfoVO;
import com.zfzs.post.modules.post.domain.PostTerminalDistribu;
import com.zfzs.post.modules.post.domain.TerminalInfo;
import com.zfzs.post.modules.post.service.PostTerminalDistribuService;
import com.zfzs.post.modules.post.service.TerminalInfoService;
import com.zfzs.post.modules.post.service.TerminalService;
import com.zfzs.post.modules.post.service.dto.TerminalInfoQueryCriteria;
import com.zfzs.post.page.JpaPageHelper;
import com.zfzs.post.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 终端实现类
 *
 * @author ck
 * @date 2018-11-23
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TerminalServiceImpl implements TerminalService {

    @Autowired
    private TerminalInfoService terminalInfoService;

    @Autowired
    private PostTerminalDistribuService postTerminalDistribuService;


    @Override
    public Map<String,Object> queryTerminalInfoPage(TerminalInfoQueryCriteria criteria,Integer page,Integer size) {
        Map<String,Object> map = new HashMap<>();
        //获取渠道ID
        Long id = SecurityUtils.getUserId();
        //管理员数据
        if (!id.equals(1L)) {
            criteria.setChannelId(id);
        }
        JpaPageHelper<TerminalInfoVO> pageHelper = terminalInfoService.findTerminalPageList(criteria,page,size);
        map.put("content",pageHelper.getResult());
        map.put("totalElements",pageHelper.getTotalCount());
        return map;
    }

    @Override
    public void deleteTerminalInfo(Long id) {
        TerminalInfo terminalInfo = terminalInfoService.findById(id);
        if (terminalInfo == null) {
            throw new BadRequestException("SN号" + terminalInfo.getSn() + "终端不存在");
        }
        terminalInfo.setDelFlag(1L);
        terminalInfoService.update(terminalInfo);
    }

    @Override
    public List<TerminalInfo> findByStartAndEnd(String startSn, String endSn, Long channelId) {
        return terminalInfoService.findByStartAndEnd(startSn, endSn,channelId);
    }

    @Override
    public void updateTerminalOwner(Long agentId, List<TerminalInfo> list) {
        String[] sn = list.stream().map(TerminalInfo::getSn).toArray(String[]::new);
        terminalInfoService.updateTerminalsAgent(agentId, sn);
    }

    @Override
    public void distribuSn(Long agentId, List<TerminalInfo> list, Long channelId) {
        List<String> snList=list.stream().map(TerminalInfo::getSn).collect(Collectors.toList());

        List<PostTerminalDistribu> disList = postTerminalDistribuService.getTerDistribuListBySnList(snList);


        if (!disList.isEmpty()) {
            for (PostTerminalDistribu d : disList) {
                d.setAcceptId(agentId);
                d.setState(1);
                d.setReceiveState(1);
                d.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            }

            postTerminalDistribuService.updateDisList(disList);

            String[] disSnList = disList.stream().map(PostTerminalDistribu::getSn).toArray(String[]::new);

            insertDistribu(agentId, channelId, disSnList);


        }

    }

    @Override
    public void insertDistribu (Long agentId, Long channelId, String...snList){
        for (String s : snList) {
            PostTerminalDistribu distribu = new PostTerminalDistribu();
            distribu.setSn(s);
            distribu.setAgentId(agentId);
            distribu.setCreateTime(new Timestamp(System.currentTimeMillis()));
            distribu.setState(2);
            distribu.setReceiveState(1);
            distribu.setChannelId(channelId);
            postTerminalDistribuService.create(distribu);
        }

    }
}
